import readline from 'node:readline';
import { AgentRunner } from './agent.js';
import { weatherTool } from './tools/weatherTool.js';

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const agent = new AgentRunner([weatherTool]);

function ask() {
    rl.question('\n🗣️ 你可以这样问我（例如：请告诉我杭州天气）：\n> ', async (q) => {
        if (!q.trim() || q.trim().toLowerCase() === 'exit') {
            rl.close();
            return;
        }
        console.log('📡 我去查一下哈……');
        try {
            const out = await agent.run(q.trim());
            console.log(`\n${decorate(out)}\n`);
        } catch (err: any) {
            console.error('❌ 出错啦：', err?.response?.data ?? err?.message ?? err);
        }
        ask();
    });
}

function decorate(text: string) {
    // 简单的美化输出
    return text
        .split('\n')
        .map(line => (line.startsWith('未找到') || line.startsWith('获取') ? `⚠️ ${line}` : `🌤 ${line}`))
        .join('\n');
}

ask();
